from django.shortcuts import render

# Create your views here.
from utils.common_mixin import APIListModelMixin, APIRetrieveModelMixin
from .models import *
from rest_framework.viewsets import GenericViewSet
from .serializer import CourseCategorySerializer, CourseSerializer, CourseDetailSerializer
from .pagination import CommPageNumberPagination
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from rest_framework.filters import SearchFilter


class CourseCategoryView(GenericViewSet, APIListModelMixin):
    queryset = CourseCategory.objects.all().filter(is_delete=False, is_show=True)
    serializer_class = CourseCategorySerializer
    cache_key = 'category_list'


class CourseView(GenericViewSet, APIListModelMixin, APIRetrieveModelMixin):
    queryset = Course.objects.all().filter(is_delete=False, is_show=True)
    serializer_class = CourseSerializer
    pagination_class = CommPageNumberPagination
    filter_backends = [OrderingFilter, DjangoFilterBackend]
    ordering_fields = ['id', 'students', 'price']
    filterset_fields = ['course_category']

    def get_serializer_class(self):
        if self.action == 'retrieve':
            return CourseDetailSerializer
        else:
            return CourseSerializer


class SearchCourse(GenericViewSet, APIListModelMixin):
    queryset = Course.objects.filter(is_delete=False, is_show=True).all()
    serializer_class = CourseSerializer
    pagination_class = CommPageNumberPagination
    filter_backends = [SearchFilter]
    search_fields = ['name']
